package com.atguigu.controller;

import com.atguigu.result.RetVal;
import com.atguigu.search.Product;
import com.atguigu.search.SearchParam;
import com.atguigu.search.SearchResponseVo;
import com.atguigu.service.SearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * SearchController
 *
 * @author yangchao
 * @version 开发阶段
 * @description TODO
 * @date 2022/2/28 19:03
 */
@RestController
@RequestMapping("/search")
@Slf4j
public class SearchController {
    @Autowired
    private  SearchService searchService;


    @Autowired
    private ElasticsearchRestTemplate restTemplate;
    //1.创建索引
    @GetMapping("createIndex")
    public RetVal createIndex() {
        // 调用类中方法自动创建
        restTemplate.createIndex(Product.class);
        restTemplate.putMapping(Product.class);
        return RetVal.ok();
    }
    //2.上架商品
    @GetMapping("onSale/{skuId}")
    public RetVal onSale(@PathVariable Long skuId){
        searchService.onSale(skuId);
        log.info("商品上架SkuId:"+skuId);
        return RetVal.ok();
    }
    //3.商品下架
    @GetMapping("offSale/{skuId}")
    public RetVal offSale(@PathVariable Long skuId){
        searchService.offSale(skuId);
        log.info("商品下架SkuId:"+skuId);
        return RetVal.ok();
    }

    //4.商品的热度排名
    @GetMapping("incrHotScore/{skuId}")
    public RetVal incrHotScore(@PathVariable Long skuId){
        searchService.incrHotScore(skuId);
        return RetVal.ok();
    }
    //5.商品搜索
    @PostMapping
    public RetVal searchProduct(@RequestBody SearchParam searchParam){
        SearchResponseVo responseVo = searchService.searchProduct(searchParam);
        return RetVal.ok(responseVo);
    }





}
